package work.soho.chat.biz.service.impl;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import work.soho.chat.api.payload.ChatMessage;
import work.soho.chat.biz.domain.ChatCustomerService;
import work.soho.chat.biz.domain.ChatSession;
import work.soho.chat.biz.domain.ChatSessionUser;
import work.soho.chat.biz.enums.ChatSessionEnums;
import work.soho.chat.biz.mapper.ChatCustomerServiceMapper;
import work.soho.chat.biz.mapper.ChatSessionMapper;
import work.soho.chat.biz.mapper.ChatSessionUserMapper;
import work.soho.chat.biz.service.ChatSessionService;
import work.soho.common.core.util.JacksonUtils;
import work.soho.longlink.api.sender.Sender;

import java.time.LocalDateTime;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Log4j2
@Service
public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatSession>
    implements ChatSessionService{

    private final ChatSessionUserMapper chatSessionUserMapper;

    private final ChatCustomerServiceMapper chatCustomerServiceMapper;

    /**
     * 消息发送接口
     */
    private final Sender sender;

    @Value("#{@sohoConfig.getByKey('chat-default-customer-service-avatar')}")
    private String defaultCustomerAvatar;

    /**
     * 查询客服会话
     *
     * @param uid
     * @param toUid
     * @return
     */
    @Override
    public ChatSession findCustomerServiceSession(Long uid, Long toUid) {
        LambdaQueryWrapper<ChatSessionUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChatSessionUser::getUserId, uid);
        List<ChatSessionUser> chatSessionUserList = chatSessionUserMapper.selectList(lambdaQueryWrapper);
        List<Long> sessionIdList = chatSessionUserList.stream().map(ChatSessionUser::getSessionId).collect(Collectors.toList());
        if(sessionIdList == null || sessionIdList.size() == 0) {
            return null;
        }

        LambdaQueryWrapper<ChatSessionUser> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(ChatSessionUser::getSessionId, sessionIdList);
        lambdaQueryWrapper1.eq(ChatSessionUser::getUserId, toUid);
        List<ChatSessionUser> chatSessionUserList1 = chatSessionUserMapper.selectList(lambdaQueryWrapper1);
        List<Long> sessionIdList1 = chatSessionUserList1.stream().map(ChatSessionUser::getSessionId).collect(Collectors.toList());
        if(sessionIdList1 == null || sessionIdList1.size() == 0) {
            return null;
        }

        //查询客服会话
        LambdaQueryWrapper<ChatSession> chatSessionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chatSessionLambdaQueryWrapper.in(ChatSession::getId, sessionIdList1);
        chatSessionLambdaQueryWrapper.eq(ChatSession::getType, ChatSessionEnums.Type.CUSTOMER_SERVICE.getId());
        chatSessionLambdaQueryWrapper.eq(ChatSession::getStatus, ChatSessionEnums.Status.ACTIVE.getId());
        chatSessionLambdaQueryWrapper.orderByDesc(ChatSession::getId);
        chatSessionLambdaQueryWrapper.last(" limit 1");
        return getOne(chatSessionLambdaQueryWrapper);
    }

    /**
     * 创建会话
     *
     * @param uid
     * @param uids
     * @param type
     * @return
     */
    public ChatSession createSession(Long uid, List<Long> uids, ChatSessionEnums.Type type) {
        ChatSession chatSession = new ChatSession();
        chatSession.setStatus(ChatSessionEnums.Status.ACTIVE.getId());
        chatSession.setCreatedTime(LocalDateTime.now());
        chatSession.setUpdatedTime(LocalDateTime.now());
        chatSession.setType(type.getId());
        chatSession.setTitle("会话"); //生成会话名字
        chatSession.setAvatar(defaultCustomerAvatar);
        save(chatSession);

        uids.add(uid);

        //保存会话用户
        for (Long toUid: uids) {
            //获取会话对方昵称
            String title = "";
            if(
                    type.getId() == ChatSessionEnums.Type.CUSTOMER_SERVICE.getId()
                    || type.getId() == ChatSessionEnums.Type.PRIVATE_CHAT.getId()
            ) {
                Optional<Long> firstNotEqual = uids.stream()
                        .filter(u -> !u.equals(uid))
                        .findFirst();
                firstNotEqual.ifPresentOrElse(
                        value -> {

                        },
                        () -> {
                            //ignore
                        }
                );
            } else {
                //TODO 组群聊，创建群聊头像
            }

            ChatSessionUser chatSessionUser = new ChatSessionUser();
            chatSessionUser.setSessionId(chatSession.getId());
            chatSessionUser.setUserId(toUid);
            chatSessionUser.setUpdatedTime(LocalDateTime.now());
            chatSessionUser.setCreatedTime(LocalDateTime.now());
            chatSessionUserMapper.insert(chatSessionUser);
        }

        return chatSession;
    }

    /**
     * 查找会话客服ID
     *
     * @param sessionId
     * @return
     */
    @Override
    public ChatSessionUser findCustomerService(Long sessionId) {
        //获取所有客服
        List<ChatCustomerService> chatCustomerServiceList = chatCustomerServiceMapper.selectList(new LambdaQueryWrapper<>());
        List<Long> customerServiceUids = chatCustomerServiceList.stream().map(ChatCustomerService::getUserId).collect(Collectors.toList());

        LambdaQueryWrapper<ChatSessionUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChatSessionUser::getSessionId, sessionId);
        List<ChatSessionUser> list = chatSessionUserMapper.selectList(lambdaQueryWrapper);
        AtomicReference<ChatSessionUser> chatSessionUser = new AtomicReference<>();
        list.forEach(item->{
            if(customerServiceUids.contains(item.getUserId())) {
                chatSessionUser.set(item);
            }
        });
        return chatSessionUser.get();
    }

    /**
     * 查询指定会话的用户信息
     *
     * @param sessionId
     * @return
     */
    public List<ChatSessionUser> getSessionUser(String sessionId) {
        LambdaQueryWrapper<ChatSessionUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChatSessionUser::getSessionId, sessionId);
        return chatSessionUserMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 聊天处理
     *
     * @param inputChatMessage
     */
    public void chat(ChatMessage inputChatMessage) {
       List<ChatSessionUser> sessionUsers = getSessionUser(inputChatMessage.getToSessionId());
       for(ChatSessionUser chatSessionUser: sessionUsers) {
           log.info("当前转发消息用户信息： {}", chatSessionUser);
           //检查是否为发送用户
           if(chatSessionUser.getUserId().equals(Long.valueOf(inputChatMessage.getFromUid()))) {
               //这是发送人
               //ignore
           } else {
               //推送消息到信道给到指定用户
               sender.sendToUid(String.valueOf(chatSessionUser.getUserId()), JacksonUtils.toJson(inputChatMessage));
           }
       }
    }
}
